using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using NET.Library.Types;
using NET.Library.Data;

namespace NET.Library.Web.Forms.DataControls.GridViews
{
    /// <summary>
    /// Populate variable types with values from GridView control data items.
    /// </summary>
    public static class GridViewDataItems
    {
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridView control data item as a string type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static string StringDataItem(string fieldName, GridViewRowEventArgs e)
        {
            return DataItem(fieldName, e).ToString();
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridViewRow data item as a string type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static string StringDataItem(string fieldName, GridViewRow row)
        {
            return DataItem(fieldName, row).ToString();
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridView control data item as a DateTime type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static DateTime DateTimeDataItem(string fieldName, GridViewRowEventArgs e)
        {
            return (DateTime)DataItem(fieldName, e);
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridViewRow data item as a DateTime type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static DateTime DateTimeDataItem(string fieldName, GridViewRow row)
        {
            return (DateTime)DataItem(fieldName, row);
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridView control data item as an integer type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static int IntDataItem(string fieldName, GridViewRowEventArgs e)
        {
            object dataItem = DataItem(fieldName, e);
            int result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                result = (int)dataItem;
            }

            return result;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridViewRow data item as an integer type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static int IntDataItem(string fieldName, GridViewRow row)
        {
            object dataItem = DataItem(fieldName, row);
            int result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                result = (int)dataItem;
            }

            return result;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridView control data item used as a Boolean type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static bool BoolDataItem(string fieldName, GridViewRowEventArgs e)
        {
           // return (bool)DataItem(fieldName, e);
            object Object = DataItem(fieldName, e);
            return Object == DBNull.Value ? false : Convert.ToBoolean(Object);
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridViewRow data item used as a Decimal type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static bool BoolDataItem(string fieldName, GridViewRow row)
        {
            //return (bool)DataItem(fieldName, row);
            object Object = DataItem(fieldName, row);
            return Object == DBNull.Value ? false : Convert.ToBoolean(Object);
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridView control data item used as a Decimal type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static decimal DecimalDataItem(string fieldName, GridViewRowEventArgs e)
        {
            object dataItem = DataItem(fieldName, e);
            decimal result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                //if (Integers.IsNumeric(dataItem.ToString()))
                //{
                result = (decimal)dataItem;
                //}
            }

            return result;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// Return GridViewRow data item used as a Decimal type.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static decimal DecimalDataItem(string fieldName, GridViewRow row)
        {
            object dataItem = DataItem(fieldName, row);
            decimal result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                //if (Integers.IsNumeric(dataItem.ToString()))
                //{
                result = (decimal)dataItem;
                //}
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static byte[] ImageDataItem(string fieldName, GridViewRowEventArgs e)
        {
            object dataItem = DataItem(fieldName, e);
            return Bytes.byteGetValue(dataItem);
            //return DBHelper.imageValue(dataItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public static byte[] ImageDataItem(string fieldName, GridViewRow row)
        {
            object dataItem = DataItem(fieldName, row);
            return Bytes.byteGetValue(dataItem);
            //rreturn DBHelper.imageValue(dataItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static double DoubleDataItem(string fieldName, GridViewRowEventArgs e)
        {
            object dataItem = DataItem(fieldName, e);
            double result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                //if (Integers.IsNumeric(dataItem.ToString()))
                //{
                result = (double)dataItem;
                //}
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public static double DoubleDataItem(string fieldName, GridViewRow row)
        {
            object dataItem = DataItem(fieldName, row);
            double result = 0;

            if (!String.IsNullOrEmpty(dataItem.ToString()))
            {
                //if (Integers.IsNumeric(dataItem.ToString()))
                //{
                result = (double)dataItem;
                //}
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Guid GuidDataItem(string fieldName, GridViewRowEventArgs e)
        {
            object dataItem = DataItem(fieldName, e);
            return Guids.guidGetValue(dataItem);
            //return DBHelper.guidValue(dataItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public static Guid GuidDataItem(string fieldName, GridViewRow row)
        {
            object dataItem = DataItem(fieldName, row);
            return Guids.guidGetValue(dataItem);
            //return DBHelper.guidValue(dataItem);
        }

        #region Private Methods
        private static object DataItem(string fieldName, GridViewRowEventArgs e)
        {
            return DataBinder.Eval(e.Row.DataItem, fieldName);
        }

        private static object DataItem(string fieldName, GridViewRow row)
        {
            return DataBinder.Eval(row.DataItem, fieldName);
        }
        #endregion
    }
}
